package com.maycur.ctrip;

import Ice.Application;
import Ice.ObjectAdapter;
import Ice.Util;
import com.maycur.ctrip.servant.CtripService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * Created by zhouchunjie on 16/2/25.
 */
@Component
@ConfigurationProperties(prefix="ice")
public class IceServer extends Application {

    private static final Logger logger = LoggerFactory.getLogger(IceServer.class);

    @Autowired
    private CtripService servant;

    private String endpoint;

    public void setEndpoint(String endpoint) {
        this.endpoint = endpoint;
    }

    @Override
    public int run(String[] strings) {
        shutdownOnInterrupt();

        ObjectAdapter oa = communicator().createObjectAdapterWithEndpoints("CtripService", endpoint);
        oa.add(servant, Util.stringToIdentity(servant.getClass().getSimpleName()));
        oa.activate();
        logger.info("Ctrip Ice Server started with {}", endpoint);
        communicator().waitForShutdown();
        return 0;
    }
}
